<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/12
 * Time: 14:02
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;

class Message extends AdminBase
{
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            $list = Db::name('message')
                ->field('id, nickname, mobile, email, title, description, content, status, create_time, update_time')
                ->order('create_time desc')
                ->paginate(20);
            $pages = $list->render();
            return view('index', [
                'list'=>$list,
                'pages'=>$pages
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }

    }

    public function changeMessageStatus()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'changeMessageStatus', $this->_juri);
            if ($chk['status'])
            {
                $id     = intval(input('param.id'));
                $status = intval(input('param.status'));
                $res    = Db::name('message')
                    ->where('id', $id)
                    ->update(['status'=>$status, 'update_time'=>time()]);
                if ($res)
                {
                    return ['status'=>true, 'msg'=>''];
                } else {
                    return ['status'=>false, 'msg'=>'审核失败'];
                }
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }

    public function del()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'del', $this->_juri);
            if ($chk['status'])
            {
                $id     = intval(input('param.id'));
                $res    = Db::name('message')
                    ->where('id', $id)
                    ->delete();
                if ($res)
                {
                    return ['status'=>true, 'msg'=>''];
                } else {
                    return ['status'=>false, 'msg'=>'删除失败'];
                }
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }
}